package view
{
	import flash.events.Event;
	
	import model.proxy.FormationProxy;
	import model.Formation;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import view.components.FormationPanel;

	public class FormationPanelMediator extends Mediator implements IMediator 
	{ 
		
		public static const NAME : String = 'FormationPanelMediator'; 
			
		/** 
		 * Constructor 
		 * Listen event of the component to send notification 
		 */ 
		 public function FormationPanelMediator(viewComponent : FormationPanel) 
		 { 
		 	super(NAME, viewComponent); 
		 	viewComponent.addEventListener(FormationPanel.OBTENIR_LISTE_FORMATIONS, obtenirListeFormationsNotification);
		 	viewComponent.addEventListener(FormationPanel.VALIDER_AJOUT_FORMATION, validerAjoutFormationNotification);	
		 	viewComponent.addEventListener(FormationPanel.VALIDER_MODIF_FORMATION, validerModifFormationNotification);
		 	viewComponent.addEventListener(FormationPanel.SUPPRIMER_FORMATION, supprimerFormationNotification);
		 } 
		 
		 
		 private function obtenirListeFormationsNotification(event : Event=null):void 
		 { 
			 sendNotification(FormationPanel.OBTENIR_LISTE_FORMATIONS, null); 
		 } 
		 
		 /** 
		 * Send a formation notification with a formationVO loaded with the values entered 
		 */ 
		 private function validerAjoutFormationNotification(event : Event=null):void 
		 { 
		 	 var formation:Formation = new Formation(); 
			 formation.nom = formationPanel.nom.text;
			 formation.objectif = formationPanel.objectif.text;
			 formation.contenu = formationPanel.contenu.text; 
			 sendNotification(FormationPanel.VALIDER_AJOUT_FORMATION, formation); 
		 } 
		 
		 private function validerModifFormationNotification(event : Event=null):void 
		 { 
			 var formation:Formation = formationPanel.affichage.selectedItem.valueOf(); 
			 formation.nom = formationPanel.nom.text;
			 formation.objectif = formationPanel.objectif.text;
			 formation.contenu = formationPanel.contenu.text; 
			 sendNotification(FormationPanel.VALIDER_MODIF_FORMATION, formation); 
		 } 
		 
		 private function supprimerFormationNotification(event : Event=null):void
		 {
		 	var listeFormations:Array = formationPanel.affichage.selectedItems;
		 	sendNotification(FormationPanel.SUPPRIMER_FORMATION, listeFormations);
		 }
		 
		 /** 
		 * List of the notifications catched by this mediator 
		 */ 
		 override public function listNotificationInterests():Array 
		 { 
		 	return [ FormationProxy.LISTE_FORMATIONS_OBTENUE ]; 
		 } 
		 
		 /** 
		 * Actions done when a notifications is catched 
		 */ 
		 override public function handleNotification(note : INotification):void 
		 { 
			 switch (note.getName()) 
			 { 
			 	case FormationProxy.LISTE_FORMATIONS_OBTENUE:
			 	{
			 		formationPanel.affichage.dataProvider = (note.getBody() as Array);	
			 		break;
			 	}
			 	
			 	default:
			 	break;
		 	} 
		 } 
		 
		 /** 
		 * Get the loginPanel component 
		 */ 
		 private function get formationPanel():FormationPanel 
		 { 
		 	return viewComponent as FormationPanel; 
		 }
	}
}